package com.speedy.course.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import com.speedy.course.R
import com.speedy.course.databinding.PublicTitleBarBinding

/**
 *
 */
class TitleLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    @AttrRes defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val viewBinding: PublicTitleBarBinding by lazy {
        PublicTitleBarBinding.inflate(LayoutInflater.from(context), this, true)
    }

    val tvTitle by lazy {
        viewBinding.tvText
    }

    val ivPublicBack by lazy {
        viewBinding.ivBack
    }

    val rightTitle by lazy {
        viewBinding.tvRightText
    }
    init {
        viewBinding.apply {
            val ta =
                context.theme.obtainStyledAttributes(
                    attrs,
                    R.styleable.TitleLayout,
                    defStyleAttr,
                    0
                )
            val title = ta.getString(R.styleable.TitleLayout_title)
            val noBack = ta.getBoolean(R.styleable.TitleLayout_noBack, false)
            if (noBack) {
                ivBack.visibility = GONE
            }
            tvText.text = title

            val rightStr = ta.getString(R.styleable.TitleLayout_rightTitle)
            tvRightText.text = rightStr
            ta.recycle()
        }
    }
}